package ad.common 
{
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	/**
	 * applyInputMask
	 * @producer digitalbox.cc
	 * @version ActionScript 3.0
	 * @author Adrian C. Miranda
	 * @mail ad@adrianmiranda.com.br
	 */
	public function applyInputMask(textField:TextField, maskFormat:String) 
	{
		textField.restrict = '0-9';
		textField.maxChars = maskFormat.length;
		textField.text = '';
		
		var inputChar:Array = [];
		for (var id:int = 0; id < maskFormat.length; id++)
		{
			if (maskFormat.charAt(id) != '0')
			{
				inputChar.push( { gap:id, character:maskFormat.charAt(id) } );
			}
		}
		
		textField.addEventListener(KeyboardEvent.KEY_DOWN, function(evt:KeyboardEvent):void 
		{
			for (var id:int = 0; id < inputChar.length; id++)
			{
				if (evt.keyCode != Keyboard.BACKSPACE && evt.keyCode != Keyboard.DELETE) 
				{
					if (evt.target.length == inputChar[id].gap)
					{
						evt.target.appendText(inputChar[id].character);
						evt.target.setSelection(inputChar[id].gap + 1, inputChar[id].gap + 1);
					}
				}
			}
		}, false, 0, true);
	}
	
}